package com.xuxueli.applyModules.utils;

import org.apache.commons.lang3.StringUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.regex.Pattern;

/**
 * @CLassName MD5Util
 * @Description MD5加解密工具
 **/
public class MD5Util {

    //MD5加密
    public static String md5Encode(String plainText) {
        byte[] secretBytes;
        try {
            secretBytes = MessageDigest.getInstance("md5").digest(
                plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("无此md5算法！");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        md5code = StringUtils.leftPad(md5code, 32, "0");
        return md5code;
    }

    /**
     * 公钥加密
     */
    public static byte[] encryptByPublicKey(RSAPublicKey publicKey, byte[] plainTextData)
        throws Exception {
        if (publicKey == null) {
            throw new Exception("加密公钥为空, 请设置");
        }
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] output = cipher.doFinal(plainTextData);
            return output;
        } catch (NoSuchAlgorithmException e) {
            throw new Exception("无此加密算法");
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            return null;
        } catch (InvalidKeyException e) {
            throw new Exception("加密公钥非法,请检查");
        } catch (IllegalBlockSizeException e) {
            throw new Exception("明文长度非法");
        } catch (BadPaddingException e) {
            throw new Exception("明文数据已损坏");
        }
    }

    //私钥解密
    public static byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData)
        throws Exception {
        if (privateKey == null) {
            throw new Exception("解密私钥为空, 请设置");
        }
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] output = cipher.doFinal(cipherData);
            return output;
        } catch (NoSuchAlgorithmException e) {
            throw new Exception("无此解密算法");
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            return null;
        } catch (InvalidKeyException e) {
            throw new Exception("解密私钥非法,请检查");
        } catch (IllegalBlockSizeException e) {
            throw new Exception("密文长度非法");
        } catch (BadPaddingException e) {
            throw new Exception("密文数据已损坏");
        }
    }

    public static void main(String[] args) {
        String message = "phone=18751682526&1578894072000&50d36561630c4ce1bb22368697c6afb1";
        String regexp = "^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478]\\d{8})$|^1705\\d{7}$";
        Pattern pattern = Pattern.compile(regexp);
        System.out.println(MD5Util.md5Encode(message));
    }
}
